/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaPasosIniciales.java
 *
 * Created on 8/01/2011, 03:50:17 PM
 */
package ventanas.pasosiniciales;

import beans.Empresa;
import beans.Usuario;
import db.Model;
import java.io.File;
import javax.swing.JOptionPane;
import ventanas.InicioSesion;

/**
 *
 * @author Usuario
 */
public class VistaPasosIniciales extends javax.swing.JFrame {

    /** Creates new form VistaPasosIniciales */
    private FormularioEmpresa formEmp;
    private FormularioUsuario formUs;
    private int paso;

    public VistaPasosIniciales() {
        correrInsertsIniciales();
        initComponents();
        paso = interfaces.Constantes.PASO_BIENVENIDO;
        btnanterior.setVisible(false);
        this.setSize(810, 480);
        this.setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbltitulo = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnsiguiente = new javax.swing.JButton();
        btncancelar1 = new javax.swing.JButton();
        btnanterior = new javax.swing.JButton();
        lblcontenido = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        getContentPane().setLayout(null);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_01.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 406, 168);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_02.png"))); // NOI18N
        jLabel2.setText("jLabel2");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(406, 0, 394, 125);

        lbltitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_05.png"))); // NOI18N
        lbltitulo.setText("jLabel3");
        getContentPane().add(lbltitulo);
        lbltitulo.setBounds(0, 168, 310, 147);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_06.png"))); // NOI18N
        jLabel5.setText("jLabel5");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(310, 167, 96, 246);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_07.png"))); // NOI18N
        jLabel6.setText("jLabel6");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(0, 315, 310, 98);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_08.png"))); // NOI18N
        getContentPane().add(jLabel7);
        jLabel7.setBounds(406, 394, 364, 19);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_04.png"))); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(770, 125, 30, 288);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        btnsiguiente.setText("Siguiente");
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });

        btncancelar1.setText("Cancelar");
        btncancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelar1ActionPerformed(evt);
            }
        });

        btnanterior.setText("Anterior");
        btnanterior.setToolTipText("");
        btnanterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnanteriorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(530, Short.MAX_VALUE)
                .addComponent(btnanterior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnsiguiente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btncancelar1)
                .addGap(31, 31, 31))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btncancelar1)
                    .addComponent(btnsiguiente)
                    .addComponent(btnanterior))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2);
        jPanel2.setBounds(0, 413, 800, 40);

        lblcontenido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_03.png"))); // NOI18N
        lblcontenido.setText("jLabel4");
        getContentPane().add(lblcontenido);
        lblcontenido.setBounds(406, 125, 364, 269);

        pack();
    }// </editor-fold>//GEN-END:initComponents

   private void correrInsertsIniciales() {
             utilidades.Reporte rep = new utilidades.Reporte();
             rep.executeSqlScript(rep.getConexion(),new File("InsertIniciales.sql"));
    }

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
        // TODO add your handling code here:
        // this.getContentPane().remove(lblcontenido);
        switch (paso) {
            case interfaces.Constantes.PASO_BIENVENIDO:
                this.irPasoFormularioEmpresa(interfaces.Constantes.PASO_SIGUIENTE);
                break;
            case interfaces.Constantes.PASO_FORMULARIO_EMPRESA:
                if (formEmp.validar()) {
                    this.irPasoFormularioUsuario(interfaces.Constantes.PASO_SIGUIENTE);
                }
                break;
            case interfaces.Constantes.PASO_FORMULARIO_USUARIO:
                if (formUs.validar()) {
                    this.irPasoFinalizar();
                }
                break;
            case interfaces.Constantes.PASO_FINALIZAR:
                this.finalizar();
                break;
        }

    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btncancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelar1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btncancelar1ActionPerformed

    private void btnanteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnanteriorActionPerformed
        // TODO add your handling code here:
        switch (paso) {
            case interfaces.Constantes.PASO_FINALIZAR:
                this.irPasoFormularioUsuario(interfaces.Constantes.PASO_ANTERIOR);
                break;
            case interfaces.Constantes.PASO_FORMULARIO_USUARIO:
                this.irPasoFormularioEmpresa(interfaces.Constantes.PASO_ANTERIOR);
                break;
            case interfaces.Constantes.PASO_FORMULARIO_EMPRESA:
                this.irPasoBienvenido();
                break;

        }
    }//GEN-LAST:event_btnanteriorActionPerformed

    public void irPasoBienvenido() {
        btnanterior.setVisible(false);
        formEmp.setVisible(false);
        this.getContentPane().remove(formEmp);
        lblcontenido.setVisible(true);
        lbltitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pasos_05.png")));
        paso = interfaces.Constantes.PASO_BIENVENIDO;
    }

    public void irPasoFormularioEmpresa(int acion) {
        btnanterior.setVisible(true);
        if (acion == interfaces.Constantes.PASO_SIGUIENTE) {
            lblcontenido.setVisible(false);
            formEmp = new FormularioEmpresa();
            formEmp.setBounds(406, 125, 364, 269);
            this.getContentPane().add(formEmp);
        }

        if (acion == interfaces.Constantes.PASO_ANTERIOR) {
            formUs.setVisible(false);
            this.getContentPane().remove(formUs);
        }

        lbltitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paso_formulario_empresa.png")));
        formEmp.setVisible(true);
        paso = interfaces.Constantes.PASO_FORMULARIO_EMPRESA;

    }

    public void irPasoFormularioUsuario(int acion) {

        if (acion == interfaces.Constantes.PASO_SIGUIENTE) {
            formEmp.setVisible(false);
            formUs = new FormularioUsuario();
            formUs.setBounds(406, 125, 364, 269);
            this.getContentPane().add(formUs);
        }

        if (acion == interfaces.Constantes.PASO_ANTERIOR) {
            lblcontenido.setVisible(false);
            btnsiguiente.setText("Siguiente");
        }

        lbltitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paso_formulario_usuario.png")));
        formUs.setVisible(true);
        paso = interfaces.Constantes.PASO_FORMULARIO_USUARIO;
    }

    public void irPasoFinalizar() {
        formUs.setVisible(false);
        lbltitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paso_finalizar.png")));
        lblcontenido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/informacion_finalizar.png")));
        lblcontenido.setVisible(true);
        btnsiguiente.setText("Finalizar");
        paso = interfaces.Constantes.PASO_FINALIZAR;
    }

    public void finalizar() {
        this.dispose();
        Model m = Model.getInstance();
        try {
            Empresa emp = formEmp.getEmpresa();
            Usuario us = formUs.getUsuario();
            m.insertarRegistro("insertarEmpresa", emp);
         //   emp=(Empresa) m.obtenerRegistro("obtenerEmpresaPorNit", new Long(emp.getNit()));
            us.setEmpresa(emp);
         
            m.insertarRegistro("insertarUsuario", us);
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    new InicioSesion().setVisible(true);
                }
            });
        } catch (Exception er) {
            JOptionPane.showMessageDialog(this, er.getMessage(), "Error Grave", JOptionPane.ERROR_MESSAGE);
            er.printStackTrace();
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VistaPasosIniciales().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnanterior;
    private javax.swing.JButton btncancelar1;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblcontenido;
    private javax.swing.JLabel lbltitulo;
    // End of variables declaration//GEN-END:variables


}
